<?php
namespace Controllers;
use Resources,
    Libraries;
class TransaksiBahan extends Libraries\PController {
    private $id, $nama_barang, $jml, $harga_satuan, $total, $id_kavling, $id_user, $ref, $id_bank, $tgl_trans;
    public function __construct() {
        parent::__construct();
        $this->db = new Libraries\Mysql();
    }
    public function index() {
        if (Libraries\u::isAjaxRequest()) {
            $transaksi_bahan = $this->db->results(Libraries\CDb::get_transaksi_bahan());
            $this->outputJSON(
                    array(
                        'success' => 'true',
                        'results' => $transaksi_bahan
            ));
        } else {
            $this->redirect('home');
        }
    }
    public function create() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $status = false;
            $msg = '';
            $this->db->begin();
            try {
                $ref = new Libraries\ReferenceCom($this->db);
                $this->ref = $ref->get_next_reference(TRANSAKSIBAHAN);
                $this->db->results(
                        "INSERT INTO psn_transaksi_bahan
            			(nama_barang, jml, harga_satuan, total, id_kavling, id_user, ref, id_bank, tgl_trans) VALUES
            			('$this->nama_barang', $this->jml, $this->harga_satuan, $this->total,$this->id_kavling,
            			$this->id_user,'$this->ref',$this->id_bank,'$this->tgl_trans');");
                $id = $this->db->insertId();
                $ref->save(TRANSAKSIBAHAN, $id, $this->ref);
                $this->db->results(
                        Libraries\CDb::save_bank_trans(TRANSAKSIBAHAN, $id,
                                $this->ref, $this->tgl_trans, -$this->total,
                                $this->id_bank, '-', $this->id_user,
                                $this->id_kavling));
                $this->db->commit();
            } catch (Exception $e) {
                $this->db->rollback();
                $status = false;
                $msg = $e->getMessage();
            }
            $id = $this->db->insertId();
            $msg = "Data berhasil di simpan dengan referensi $this->ref";
            $this->outputJSON(array(
                'success' => $status,
                'msg' => $msg
            ));
        } else {
            $this->redirect('home');
        }
    }
    public function view() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $transaksi = $this->db->results(Libraries\CDb::view_transaksi_bahan($this->id));
            $this->outputJSON(array('success' => 'true', 'results' => $transaksi));
        } else {
            $this->redirect('home');
        }
    }
    public function delete() {
        if (Libraries\u::isAjaxRequest()) {
            $id = $this->get_post('id');
            $memo_ = $this->get_post('memo_');
            $status = false;
            $msg = 'Transaksi bahan berhasil divoid.';
            $user_id = Libraries\u::get_user_id($this->session);
            $this->db->begin();
            try {
                $ref = new Libraries\ReferenceCom($this->db);
                $this->doc_ref = $ref->get_next_reference(VOID);
                $trans_bahan = $this->db->row(Libraries\CDb::get_transaksi_bahan_by_id($id));
                $this->db->results(Libraries\CDb::insert_voided(TRANSAKSIBAHAN,
                                $trans_bahan->id_transaksi_bahan,
                                $trans_bahan->tgl_trans, $memo_));
                $id = $this->db->insertId();
                $ref->save(VOID, $id, $this->doc_ref);
                $this->db->results(
                        Libraries\CDb::save_bank_trans(VOID, $trans_bahan->id_transaksi_bahan,
                                $trans_bahan->doc_ref, $trans_bahan->tgl_trans,
                                $trans_bahan->total, $trans_bahan->id_bank, '-',
                                $user_id, $trans_bahan->id_kavling));
                $this->db->commit();
            } catch (Exception $e) {
                $this->db->rollback();
                $status = false;
                $msg = $e->getMessage();
            }
            $this->outputJSON(array(
                'success' => $status,
                'msg' => $msg
            ));
        } else {
            $this->redirect('home');
        }
    }
    private function get_submit() {
        foreach ($_POST as $key => $val) {
            $_POST[$key] = $this->request->post($key,
                    FILTER_SANITIZE_MAGIC_QUOTES);
            $_POST[$key] = $this->db->escape($_POST[$key]);
            $this->$key = $val;
        }
        $this->harga_satuan = Libraries\u::get_number($this->harga_satuan);
        $this->jml = Libraries\u::get_number($this->jml);
        $this->total = Libraries\u::get_number($this->total);
        $this->id_user = Libraries\u::get_user_id($this->session);
    }
}
